import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

import Home from '../views/home'
import Login from '../views/Individual/login'
import Register from '../views/Individual/register'
import Search from '../views/Individual/search'
import Jobs from '../views/Individual/jobs'
import MyResume from '../views/Individual/myResume'
import ResumeEdit from '../views/Individual/resumeEdit'
import ResumeAdd from '../views/Individual/resumeAdd'
import LoginEnterprise from '../views/Enterprise/loginEnterprise'
import RegisterEnterprise from '../views/Enterprise/registerEnterprise'
import EnterpriseResume from '../views/Enterprise/enterpriseResume'
import EnterpriseHome from '../views/Enterprise/enterpriseHome'
import EnterpriseInfo from '../views/Enterprise/enterpriseInfo'
import EnterpriseRecruit from '../views/Enterprise/enterpriseRecruit'

//保存原型对象push
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
//重写push|replace
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => {}, () => {});
  }
}
VueRouter.prototype.replace = function (location,resolve,reject){
  if (resolve && reject){
    originReplace.call(this,location,resolve,reject);
  }else {
    originReplace.call(this,location,()=>{},()=>{});
  }
}

let router = new VueRouter({
  routes: [
    {
      path: '/home',
      component: Home,
      meta: {show: true,header:true,Eheader:false}
    },
    {
      path: '/login',
      component: Login,
      meta: {show: false,header:true,Eheader:false}
    },
    {
      path: '/register',
      component: Register,
      meta: {show: false,header:true,Eheader:false}
    },
    {
      path: '/search/:keyword',
      component: Search,
      meta: {show: true,header:true,Eheader:false},
      name: "search"
    },
    {
      path: '/jobs',
      component: Jobs,
      meta: {show: true,header:true,Eheader:false}
    },
    {
      path: '/myResume',
      component: MyResume,
      meta: {show: true,header:true,Eheader:false}
    },
    {
      path: '/resumeEdit',
      component: ResumeEdit,
      meta: {show: true,header:true,Eheader:false}
    },
    {
      path: '/resumeAdd',
      component: ResumeAdd,
      meta: {show: true,header:true,Eheader:false}
    },
    {
      path: '/loginEnterprise',
      component: LoginEnterprise,
      meta: {show: false,header:false,Eheader:true}
    },
    {
      path: '/registerEnterprise',
      component: RegisterEnterprise,
      meta: {show: false,header:false,Eheader:true}
    },
    {
      path: '/enterpriseResume',
      component: EnterpriseResume,
      meta: {show: true,header:false,Eheader:true}
    },
    {
      path: '/enterpriseHome',
      component: EnterpriseHome,
      meta: {show: true,header:false,Eheader:true}
    },
    {
      path: '/enterpriseInfo',
      component: EnterpriseInfo,
      meta: {show: true,header:false,Eheader:true}
    },
    {
      path: '/enterpriseRecruit',
      component: EnterpriseRecruit,
      meta: {show: true,header:false,Eheader:true}
    },
    //  重定向到首页
    {
      path: '*',
      redirect: "/home"
    }
  ]
})

//全局守卫：前置守卫（在路由跳转之间进行判断）
// router.beforeEach(async (to,from,next)=>{
//   //to：可以获取到你要跳转到哪个路由信息
//   //from：可以获取到你从哪个路由而来的信息
//   //next：放行函数 next()放行 next(path)放行到指定路由
//   // next()
//   //用户登录了才有token，未登录没有
//   let token = store.state.user.token
//   //用户信息
//   let name = store.state.user.userInfo.name
//   if (token){
//     if (to.path==='/login'){
//       next('/home')
//     }else {
//       //如果已有用户名
//       if (name){
//         next()
//       }else {
//         //没有用户信息，派发action让仓库存储用户信息
//         try {
//           await store.dispatch('getUserInfo')
//           next()
//         }catch (e){
//           //token过期了，获取不到用户信息
//           //清除token
//           await store.dispatch('userLogout')
//           next('/login')
//         }
//       }
//     }
//   }else {
//     next()
//   }
//
// })

export default router